/* eslint-disable no-use-before-define */
import axios, { type Paging, type Response } from '@/utils/axios/axios';
import { getSHA256 } from '@/utils/axios/crypto-util';

/** 用户列表 分页 */
export function getAdminUserListAxios(data: AdminUserListRequest): Promise<Response<Paging<AdminUser>>> {
  return axios.get(`/system/user/page`, { params: data }) as unknown as Promise<Response<Paging<AdminUser>>>;
}

/** 用户列表 */
export function getAdminUserListNotPageAxios(): Promise<Response<AdminUser[]>> {
  return axios.get(`/system/user/list`) as unknown as Promise<Response<AdminUser[]>>;
}

/** 删除管理员用户 */
export function delAdminUserAxios(idList: number[]): Promise<Response<string>> {
  return axios.post(`/system/user/removeIds`, { idList }) as unknown as Promise<Response<string>>;
}

/** 冻结或者解冻 */
export function changeStatusAdminUserAxios(userId: number): Promise<Response<string>> {
  return axios.post(`/system/user/updateStatus`, { userId }) as unknown as Promise<Response<string>>;
}

/** 得到所有的角色列表（非分页） */
export function getAllRoleAxios(): Promise<Response<{ roleId: number; roleName: string }[]>> {
  return axios.get(`/system/role/list`) as unknown as Promise<Response<{ roleId: number; roleName: string }[]>>;
}

/** 新增管理员 */
export function addAdminUserAxios(data: AddAdminUser): Promise<Response<number>> {
  const _data = JSON.parse(JSON.stringify(data));
  _data.password = getSHA256(data.password!);
  delete _data.roleName;
  return axios.post(`/system/user/add`, _data) as unknown as Promise<Response<number>>;
}
/** 修改管理员 */
export function editAdminUserAxios(data: AddAdminUser): Promise<Response<string>> {
  const _data = JSON.parse(JSON.stringify(data));
  _data.password = _data.password ? getSHA256(data.password!) : null;
  delete _data.roleName;
  return axios.post(`/system/user/edit`, _data) as unknown as Promise<Response<string>>;
}

/** 得到角色列表（ 分页） */
export function getSysRoleListAxios(data: SysRoleListRequest): Promise<Response<Paging<SysRole>>> {
  return axios.get(`/system/role/page`, { params: data }) as unknown as Promise<Response<Paging<SysRole>>>;
}

/** 查询部门列表 分页） */
export function getSysDeptListAxios(data?: DeptRequest): Promise<Response<DeptItem[]>> {
  return axios.get(`/system/dept/list`, { params: data }) as unknown as Promise<Response<DeptItem[]>>;
}

/** 查询部门列表 分页） */
export function getSysDeptTreeAxios(): Promise<Response<DeptItemTree[]>> {
  return axios.get(`/system/dept/deptTree`) as unknown as Promise<Response<DeptItemTree[]>>;
}

/** 新增部门 */
export function addSysDeptListAxios(data: DeptItem): Promise<Response<string>> {
  return axios.post(`/system/dept/add`, data) as unknown as Promise<Response<string>>;
}

/** 修改部门 */
export function editSysDeptListAxios(data: DeptItem): Promise<Response<string>> {
  return axios.post(`/system/dept/update`, data) as unknown as Promise<Response<string>>;
}

/** 删除部门 */
export function removeSysDeptAxios(deptId: string): Promise<Response<string>> {
  return axios.delete(`/system/dept/remove`, { params: { deptId } }) as unknown as Promise<Response<string>>;
}

/** 新增角色 */
export function addRoleAxios(data: RoleRequest): Promise<Response<string>> {
  return axios.post(`/system/role`, data) as unknown as Promise<Response<string>>;
}
/** 编辑角色 */
export function editRoleAxios(data: RoleRequest): Promise<Response<string>> {
  return axios.put(`/system/role`, data) as unknown as Promise<Response<string>>;
}

/** 删除角色 */
export function delRoleAxios(roleId: number): Promise<Response<string>> {
  return axios.delete(`/system/role/remove`, { params: { roleId } }) as unknown as Promise<Response<string>>;
}

/** 停用或者启用 */
export function changeStatusRoleAxios(roleId: number): Promise<Response<string>> {
  return axios.post(`/system/role/updateStatus`, { roleId }) as unknown as Promise<Response<string>>;
}

/** 得到角色信息 */
export function getRoleMenuListAxios(roleId: number): Promise<Response<RoleMenuPermission[]>> {
  return axios.get(`/system/roleMenu/list`, { params: { roleId } }) as unknown as Promise<Response<RoleMenuPermission[]>>;
}

/** 同步菜单到数据库 */
export function menuSyncAxios(data: MenuItem[]): Promise<Response<string>> {
  return axios.post(`/system/menu/sync`, {
    menuList: data,
  }) as unknown as Promise<Response<string>>;
}

export type SysRoleListRequest = {
  /** 当前页 */
  current: number;
  /** 每页数量 */
  size: number;
  /** 角色名称 */
  roleName: string;
};
export type AdminUserListRequest = {
  /** 当前页 */
  current: number;
  /** 每页数量 */
  size: number;
  /** 状态(0-正常,1-冻结)  */
  status: number | '';
  /** 电话  */
  phone: string;
  /** 真实姓名 */
  realname: string;
  /** 登录账号 */
  username: string;
  /** 部门ID */
  deptId?: number;
};

export type BaseAdminUser = {
  /** 头像  */
  avatar: string;
  /** 生日 yyyy-MM-dd */
  birthday?: string;
  /** 电话  */
  phone: number | string;
  /** 登录账号 */
  username: string;
  /** 真实姓名 */
  realname: string;
  /** 角色 ID */
  roleId: number | '';
  /** 角色名称 */
  roleName: string;
  /** 性别(0-默认未知,1-男,2-女)  */
  sex: number;
  deptId: number | null;
};

export interface DeptRequest {
  /**
   * 部门id
   */
  deptId?: string;
  /**
   * 部门名称
   */
  deptName?: string;
  /**
   * 父部门id
   */
  parentId?: string;
  /**
   * 部门状态（0正常 1停用）
   */
  status?: string;
}

export interface DeptItemTree {
  'deptId': number;
  'parentId': number;
  'deptName': string;
  childrenDeptList: DeptItemTree;
}

export interface DeptItem {
  /**
   * 创建时间
   */
  createTime?: string | null;
  /**
   * 部门id
   */
  deptId: string;
  /**
   * 部门名称
   */
  deptName: string;
  /**
   * 显示顺序
   */
  orderNum?: string | null;
  /**
   * 父部门id
   */
  parentId?: string;
  /**
   * 部门状态（0正常 1停用）
   */
  status?: number;
}

export type AdminUser = BaseAdminUser & {
  /** 是否为超级管理员 */
  admin: boolean;
  /** 状态(0-正常,1-冻结) */
  status: 0 | 1;
  /** 创建时间 */
  createTime: string;
  /** 主键id */
  userId: number;
  deptId: number;
};

export type AddAdminUser = BaseAdminUser & {
  /** 密码 */
  password: string;
  /** 主键id */
  userId?: number;
};

export type SysRole = {
  /** 是否为超级管理员 */
  admin: boolean;
  /** 角色ID  */
  roleId: number;
  /** 角色名称  */
  roleName: string;
  /** 状态(0-正常,1-冻结) */
  status: 0 | 1;
  /** 备注 */
  remark: string;
  /** 创建时间 */
  createTime: string;
  dataScope: string;
};

export type PageCatalogue = {
  /** 菜单类型（M菜单 B按钮 N目录） */
  menuType: 'N';
  /** 路由地址 */
  path: string;
};

export type PagePermission = {
  /** 菜单类型（M菜单 B按钮） */
  menuType: 'M';
  /** 路由地址 */
  path: string;
};
export type ButtonPermission = {
  /** 菜单名称 */
  buttonName: string;
  /** 菜单类型（M菜单 B按钮） */
  menuType: 'B';
  /** 路由地址 */
  path: string;
};

export type RoleMenuPermission = ButtonPermission | PageCatalogue | PagePermission;

export type RoleRequest = {
  /** 角色ID  */
  roleId?: number;
  /** 备注  */
  remark?: string;
  /** 角色-菜单  */
  roleMenuList: RoleMenuPermission[];
  /** 角色名称 */
  roleName: string;
  dataScope: string;
};

export type MenuItem = {
  /**
   * 子菜单
   */
  children?: MenuItem[];
  /**
   * 菜单名称
   */
  menuName?: string;
  /**
   * 菜单类型（D目录 M菜单 B按钮）
   */
  menuType?: string;
  /**
   * 菜单的meta信息
   */
  meta?: {
    title: string;
  };
  /**
   * 路由地址
   */
  path?: string;
};
